package com.yuyu.qingmangmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.yuyu.qingmangmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118604048";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCnBuhLjNjNxzXgq+tv00FxuQuXpekfYKCqrOyT0JNZWfsaUNKeHDu3Pq0GGxHHMVaZjNfV/DB3E1+xfVp/7wAqd7XGh3H+i8cME7fMlG+1ld4PB2Ur2Je6Ce2Ea3U4UU8WZVShyu6ZcFDJ46GY4nsgZYWkxwjElGQ/fDnbFxoYJXs1kggcYLhb0rsuH8ZzRgi0blJiW078Mym7Sj3vXf3NFc7g/O7FHCUpQd1dAB6PnGBjvpFGj/RgJiRc4gYE4ifiINDLSIv17k3tVDG49DpUHp/Oir3H4E62pHFqxgpjypjZvFkunxdOettwZcjahl6yFDjRn42XBNcr6uzpWSvBAgMBAAECggEAbBx4d45vNqLfKNGezv76uVWBP2NQhdLRrCfJ6dNzzL4FXn86FMVJkc6rx+v1ke1JlJIpkoRuCmUr54Kv9WcJwHND4YECNDTdXd/VdEIQHIUo+qlavwqvCugqKTA064MVofVs1ly4G/ErGcKRcpgGAm40FvgNoNA8IyuRd1J4vW9UopXc09m+opgMhpDrtXHgpgQsSmJbkfw+7dTMxwrmGAUG5N7ze8ypijuIAIexr0QkPzuc8iugxWzzBJFPZmHvQOx1PRuCFMrRdDyMsr+wUaByjfNr3bRlBvKswDYlTFkWG9lZI1m0EkiBq8jy3/twc5a6nG7M1+qVPuOWJ71PwQKBgQD60mm7ym6yz5u4g6DRucYtjZiaRtZs9HTahFsMQ1bSfBF1yimsvvARaM/QYBlzf5BKsWCWgVnM462QgxsmmNCr78GeZDGooqkSv/iLB9c2uTQfi/EZ+PeRvhjnhCei/eBsgErbnlCqmPwVgimr2sZjiNLDsg/gcJ89BY2vcLLcPQKBgQCqeaPxyVL5q0Qr+iXRqOHA43TVTQmXJbOiXOIkltwMkbdfnplefgADrnR/3h/8fd8E5j5tl2pTgqob9ulLwyfgNIpF37XT5fYKNCc9D7bDuVPFxIHgJnxiIz/vdnLrYM6RIFtGsLVoUM49qTpoTw78vHCEMQFwOr//bjkAFqVx1QKBgQDduU+6sDK3YwtXSxTJER59emRiTSiUXG1IFiSebJC9tOuCYtoAWTHDUhITNECLd0LIpJCru2D03He7/gnsTr54n5NbP1sepdwsNQOzc5TuTT4kfQL/YLUJYzCEyLB5R1Ay2h5Sklrg1ChToyLPRrCFoJggYrHiR+ycduJPKqMSSQKBgB+bdAklUawW1AnvVEm+LTUtNwFBSlDq3ZZx8D/mdKAj+ELTTg5WADyugM0gav/OeqYW5mY9V6pqJI4QxoqUgXUxaJ2x7619CpQzvT62BiXd26lkWMwtdoxfisAkTwRTK5l+zU2a/9d6e/K0yK6EUfwMGDR6PZEIY4bBcfIRQNWNAoGAB3vkNv5ZMOgJUbBSzasKBhOcKHkoM3MAueJxmnh29MMfdXKlK86Rvw6PnkMRclxfTokIIJxEc+phjm8I56I4qJ/n0dX2s1KGMHpjytlqneVbhfi/EMS3TO3dTayPUl1TrnwQaNw1jpf7jRH5Qy2obi5bufF/OEVBpv4oF3KVqi0=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoDawxfaG5NFE7WiJL4rY28OBNEirjA7+9V59tQtvxi4htxLe3a7tVpPoKudVgjYdHIBM7Vg6x3IZQTqzyioq6ixoI9j++Je+1+qWjoCMBpYtYcVhY1eASNzrxZhe4h7Av4N1KUCxvoMBQoAU3WWWfN85IKKU2+aSkzHZ9iMSWE/Ift0E1Y3a7tu+N4QykHpZdrG6dwYmtfRxvL4TDc63DcYlzhViRjwOJjth3G70FQUUtt7YskUW5izeR3c5Cs/tSF8kLFAqLqzuspdqfRH0Vs8DRKhC+WgSEdO+twjF+ZNkRzPmYpU2nDOjByvvX87AmIgOZ9809uyM6DGqpY4UTwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://n98ttc.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.qingmangmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeoutExpress = "1m"; // 测试环境

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeoutExpress +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
